#!/bin/sh
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script"
    exit 1
fi
echo "========================================================================="
echo "FREEBSD APACHE PHP SETUP"
echo "========================================================================="
export PACKAGESITE=http://cvsup10.tw.freebsd.org/FreeBSD/ports/i386/packages-9.0-release/Latest/
if [ -s .install ]; then
  echo "UPDATING"
  else
 echo "install">.install
 echo 'apache_enable="YES"'>>/etc/rc.conf
 echo 'mysql_enable="YES"'>>/etc/rc.conf
 echo 'memcached_enable="YES"'>>/etc/rc.conf
 echo 'mongod_enable="YES"'>>/etc/rc.conf
 pkg_add -r autoconf268 pcre libmemcached mysql55-server memcached mongodb subversion libxml2 oniguruma curl mcrypt freetype jpeg png kbproto libICE libSM libX11 xextproto printproto libXext libXp libXt libXmu libXpm libXaw 
 fetch http://7shell.googlecode.com/svn/loader.conf
 mv loader.conf /boot/defaults/
 kldload accf_http
fi

if [ -s up.tar.gz ]; then
  echo "apache + php [found]"
  tar zxvf up.tar.gz
  else
  echo "Error: up.tar.gz not found!!!download now......"
  fetch http://7shell.googlecode.com/svn/freebsd/up.tar.gz
  tar zxvf up.tar.gz
fi

if [ -s apache.tar.gz ]; then
  echo "apache [found]"
  tar zxvf apache.tar.gz
  else
  echo "Error: apache.tar.gz not found!!!download now......"
  tar zxvf up.tar.gz
  tar zxvf apache.tar.gz
  
  
fi

if [ -s php.tar.gz ]; then
  echo "php [found]"
  tar zxvf php.tar.gz
  else
  echo "Error: php.tar.gz not found!!!download now......"
  fetch http://7shell.googlecode.com/svn/freebsd/up.tar.gz
  tar zxvf up.tar.gz
  tar zxvf php.tar.gz
  
fi

if [ -s apr.tar.gz ]; then
  echo "apr [found]"
 tar zxvf apr.tar.gz
  else
  echo "Error: apr-1.4.6.tar.gz not found!!!download now......"
  fetch http://7shell.googlecode.com/svn/freebsd/apr.tar.gz
  tar zxvf apr.tar.gz
fi

if [ -s /usr/local/apr ];then
rm -rf /usr/local/apr
fi
mv apr /usr/local/
if [ -s /usr/local/apache ];then
rm -rf /usr/local/apache
fi
mv apache /usr/local/
if [ -s /usr/local/php ];then
rm -rf /usr/local/php
fi
mv php /usr/local/
mkdir -p /var/www
chown -R www:www /var/www
echo "<?php phpinfo();?>">/var/www/index.php
if [ -s /usr/local/etc/rc.d/apache ]; then
 rm /usr/local/etc/rc.d/apache
fi
mv apache2 /usr/local/etc/rc.d/apache
chmod +x /usr/local/etc/rc.d/apache
fetch http://7shell.googlecode.com/svn/my.cnf
mv my.cnf /var/db/mysql/
pkill -9 httpd
pkill -9 mysqld
pkill -9 memcached
pkill -9 mongod
service apache start
service mysql-server start
service memcached start
service  mongod start
rm *.gz

